<template>
  <div class="login">
    <div class="box">
      <div>后台管理系统</div>
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item prop="userName">
          <el-input v-model="ruleForm.userName" placeholder="登录账号" prefix-icon="el-icon-user-solid"></el-input>
        </el-form-item>

        <el-form-item prop="pwd">
          <el-input v-model="ruleForm.pwd" placeholder="密码" prefix-icon="el-icon-lock"></el-input>
        </el-form-item>

        <el-form-item prop="imgcode">
          <el-input v-model="ruleForm.imgcode" placeholder="验证码" prefix-icon="el-icon-picture"></el-input>
        </el-form-item>

        <img class="code" :src="src" alt="">

        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import { mylogin } from "@/api/login";
export default {
  data() {
    return {
      ruleForm: {
        userName: 'hqln',
        pwd: 'zwk2000',
        imgcode: '1234',
        pdomain: "zhangzhengyan",
        k: 0
      },
      src: "",
      rules: {
        userName: [
          { required: true, message: '不能为空', trigger: 'blur' }
        ],
        pwd: [
          { required: true, message: '密码为空', trigger: 'blur' }
        ],
        imgcode: [
          { required: true, message: '验证码不对', trigger: 'blur' }
        ],
      }
    };
  },
  mounted() {
    this.getcode()
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          console.log(this.ruleForm);
          let { data: res } = await mylogin(this.ruleForm)
          console.log(res);
          sessionStorage.setItem("token", res.token)
          console.log(sessionStorage.getItem("token"));
          this.$message({
            message: '登录成功',
            type: 'success',
            duration: "2000",
          });
          setTimeout(() => {
            this.$router.push("/home")
          }, 2500)
        }
      });
    },

    getcode() {
      this.ruleForm.k = Math.random()
      this.src = "https://user.api.it120.cc/code?k=" + this.ruleForm.k
    },
  }

}
</script>
<style lang="scss" scoped>
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  background: #2d3a4d;
  height: 100vh;

  .box {
    width: 520px;
    height: 570px;
    position: relative;

    .code {
      height: 40px;
      position: absolute;
      bottom: 325px;
      right: 5px;
    }

    ::v-deep .el-form-item__content {
      margin-left: 0 !important;
      height: 50px;
      margin-bottom: 20px;
    }

    .el-button--primary {
      width: 100%;
      margin-top: 50px;

    }

    ::v-deep .el-input__inner {
      color: #fff;
      background: rgb(40, 52, 67);
    }
  }
}
</style>